package xyz.riceball.usercore.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.ApiRc;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.framework.core.valid.ValidAdd;
import xyz.riceball.framework.core.valid.ValidEdit;
import xyz.riceball.usercore.entity.po.MenuPO;
import xyz.riceball.usercore.service.MenuService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * 菜单表(Menu)表控制层
 *
 * @author xiaovcloud
 * @since 2022-04-24 20:17:43
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/rb/api/usercore/menu")
@RbSysRequest
public class MenuController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 分页查询所有数据
     *
     * @param pageParam 分页对象
     * @param menuPO    查询实体
     * @return 所有数据
     */
    @RbLog(description = "分页查询菜单", isSaveRequestData = true)
    @ApiOperation(value = "分页查询菜单")
    @GetMapping
    public ApiR<Page<MenuPO>> selectPage(PageParam pageParam, MenuPO menuPO) {
        return success(menuService.selectPage(PageParam.<MenuPO>of(pageParam), menuPO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RbLog(description = "根据id查询菜单", isSaveRequestData = true)
    @ApiOperation(value = "根据id查询菜单")
    @GetMapping("{id}")
    public ApiR<MenuPO> selectOne(@PathVariable Serializable id) {
        return success(menuService.getById(id));
    }

    /**
     * 根据name模糊查询菜单
     *
     * @param name 主键
     * @return 单条数据
     */
    @RbLog(description = "根据name模糊查询菜单", isSaveRequestData = true)
    @ApiOperation(value = "根据name模糊查询菜单")
    @GetMapping("/namelr/{name}")
    public ApiR<List<MenuPO>> selectMenuLikeName(@PathVariable String name) {
        return success(menuService.selectMenuLikeName(name));
    }

    /**
     * 新增数据
     *
     * @param menuPO 实体对象
     * @return 新增结果
     */
    @RbLog(description = "新增菜单", isSaveRequestData = true)
    @ApiOperation(value = "新增菜单")
    @PostMapping
    public ApiR<MenuPO> insert(@RequestBody @Valid @Validated(ValidAdd.class) MenuPO menuPO) {
        return menuService.add(menuPO);
    }

    /**
     * 修改数据
     *
     * @param menuPO 实体对象
     * @return 修改结果
     */
    @RbLog(description = "修改菜单", isSaveRequestData = true)
    @ApiOperation(value = "修改菜单")
    @PutMapping
    public ApiR<Void> update(@RequestBody @Valid @Validated(ValidEdit.class)  MenuPO menuPO) {
        boolean edit = menuService.edit(menuPO);
        return ApiR.msg(edit, "修改菜单成功", "修改菜单失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @RbLog(description = "删除菜单", isSaveRequestData = true)
    @ApiOperation(value = "删除菜单")
    @DeleteMapping
    public ApiRc<Void> delete(@RequestBody List<String> ids) {
        return menuService.delete(ids);
    }

    /**
     * 树形结构查询所有菜单
     *
     * @return List<Tree < String>>
     */
    @RbLog(description = "树形结构查询所有菜单")
    @ApiOperation(value = "树形结构查询所有菜单")
    @GetMapping("/tree")
    public ApiR<List<Tree<String>>> treeMenu(MenuPO menuPO) {
        return success(menuService.treeMenu(menuPO));
    }

    /**
     * 树形结构查询当前用户所见菜单
     *
     * @return List<Tree < String>>
     */
    @RbLog(description = "树形结构查询当前用户所见菜单")
    @ApiOperation(value = "树形结构查询当前用户所见菜单")
    @GetMapping("/current/tree")
    public ApiR<List<Tree<String>>> treeMenuCurrent() {
        return success(menuService.treeMenuCurrent());
    }

//    /**
//     * 前端展示勾选
//     * 返回所有没有子节点的有效的菜单id
//     *
//     * @return
//     */
//    @ApiOperation(value = "查询被选中的子集菜单")
//    @GetMapping("/valid")
//    public ApiR<List<String>> validChildMenuId() {
//        return success(menuService.validChildMenuId());
//    }
//
//    /**
//     * 修改有效的菜单
//     *
//     * @param ids
//     * @return
//     */
//    @ApiOperation(value = "修改有效的菜单")
//    @PutMapping("/valid")
//    public ApiR<List<String>> changeValidMenu(@RequestParam("ids") List<String> ids) {
//        boolean flag = menuService.changeValidMenu(ids);
//        return ApiR.msg(flag, "修改菜单成功", "修改菜单失败");
//    }
}

